package com.cade.agriculturemall.view.shoppingCar.settlement;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.cade.agriculturemall.R;
import com.cade.agriculturemall.base.BaseAdapter;
import com.cade.agriculturemall.base.BaseViewHolder;
import com.cade.agriculturemall.bean.Remark;
import com.cade.agriculturemall.contents.Contents;
import com.cade.agriculturemall.model.AddressEntity;
import com.cade.agriculturemall.model.Detail;
import com.cade.agriculturemall.model.ShoppingCarsEntity;
import com.cade.agriculturemall.model.Ticket;
import com.cade.agriculturemall.view.personalCenter.editAddress.EditAddressActivity;
import com.cade.agriculturemall.view.personalCenter.manageAddress.ManageAddressActivity;
import com.cade.agriculturemall.view.personalCenter.selectAddress.AddressSelectActivity;
import com.cade.agriculturemall.view.shoppingCar.settlement.TicketSelectDialog.TicketSelectDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lobby on 2017/12/26.
 */

public class SettlementAdapter extends BaseAdapter<BaseViewHolder> implements View.OnClickListener {
    private static final int CHECK_ALIPAY = 0;
    private static final int CHECK_WECHECK = 1;
    private static final int CHECK_DB = 2;
    private RadioButton alipayButton, wechatButton, bdButton;
    private static int radio_check = CHECK_ALIPAY;
    public String pay_channel = "";
    public String remark = "";
    private List<SettlementViewModel> models = new ArrayList<>();
    private Context mContext;
    private List<Remark> remarkList = new ArrayList<>();
    private MyClickListener myClickListener;

    public SettlementAdapter(Context context, MyClickListener myClickListener) {
        mContext = context;
        this.myClickListener = myClickListener;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseViewHolder v = null;
        switch (viewType) {
            case SettlementViewModel.TYPE_EMPTY:
                v = new BaseViewHolder(parent, R.layout.item_empty_address);
                break;
            case SettlementViewModel.TYPE_NO_FIRST:
                v = new BaseViewHolder(parent, R.layout.item_no_first);
                break;
            case SettlementViewModel.TYPE_ADRESS:
                v = new BaseViewHolder(parent, R.layout.item_settlement_address);
                break;
            case SettlementViewModel.TYPE_SJ:
                v = new BaseViewHolder(parent, R.layout.item_settlement_shoper);
                break;
            case SettlementViewModel.TYPE_CP:
                v = new BaseViewHolder(parent, R.layout.item_settlement_good);
                break;
            case SettlementViewModel.TYPE_SJ_FOOTER:
                v = new BaseViewHolder(parent, R.layout.item_settlement_footer);
                break;
            case SettlementViewModel.TYPE_TICKET:
                v = new BaseViewHolder(parent, R.layout.item_ticket);
                break;
            case SettlementViewModel.TYPE_PAY:
                v = new BaseViewHolder(parent, R.layout.item_choose_pay);
                break;
        }
        return v;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getItemCount() {
        if (models.size() != 0) {
            return models.size();
        } else {
            return 0;
        }
    }

    @Override
    public int getItemViewType(int position) {
        return models.get(position).getType();
    }

    private int currentPosition = 0;

    @Override
    public void onBindViewHolderWrapper(BaseViewHolder holder, final int position) {
        int viewType = getItemViewType(position);
        switch (viewType) {
            case SettlementViewModel.TYPE_EMPTY:
                RelativeLayout rlEmpty = holder.findViewById(R.id.item_empty_address);
                rlEmpty.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, EditAddressActivity.class);
                        intent.putExtra("type", "2");
                        ((SettlementActivity) mContext).startActivityForResult(intent, 1);
                    }
                });
                break;
            case SettlementViewModel.TYPE_NO_FIRST:
                TextView tvNoFirst = holder.findViewById(R.id.tv_item_no_first_address);
                tvNoFirst.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, ManageAddressActivity.class);
                        ((SettlementActivity) mContext).startActivityForResult(intent, 1);
                    }
                });
                break;
            case SettlementViewModel.TYPE_ADRESS:
                AddressEntity addressEntity = (AddressEntity) models.get(position).getObject();
                TextView tvConteactorName = holder.findViewById(R.id.settlement_address_contractor);
                TextView tvPhone = holder.findViewById(R.id.settlement_address_phone);
                TextView tvAddress = holder.findViewById(R.id.settlement_address_tv_address);
                if (addressEntity != null) {
                    tvConteactorName.setText(addressEntity.getContractor());
                    tvPhone.setText(addressEntity.getMobile());
                    String address = "";
                    String procincecity = addressEntity.getProvince() + addressEntity.getCity();
                    if (addressEntity.getCounty() != null) {
                        address += procincecity + addressEntity.getCounty() + addressEntity.getStress() + addressEntity.getAddress();
                    } else {
                        address += procincecity + addressEntity.getStress() + addressEntity.getAddress();
                    }
                    tvAddress.setText(address);
                }
                RelativeLayout rlAddress = holder.findViewById(R.id.settlement_rl_address);
                rlAddress.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mContext != null) {
                            Intent intent = new Intent(mContext, AddressSelectActivity.class);
                            ((SettlementActivity) mContext).startActivityForResult(intent, 1);
                        }
                    }
                });
                break;
            case SettlementViewModel.TYPE_SJ:
                String shopperName = (String) models.get(position).getObject();
                TextView tvShopperName = holder.findViewById(R.id.item_setttlement_address);
                tvShopperName.setText(shopperName);
                break;
            case SettlementViewModel.TYPE_CP:
                ShoppingCarsEntity entity = (ShoppingCarsEntity) models.get(position).getObject();
                ImageView imgShopper = holder.findViewById(R.id.item_setttlement_good_img);
                TextView tvGoodName = holder.findViewById(R.id.item_setttlement_good_tv_name);
                TextView tvGoodPrice = holder.findViewById(R.id.item_setttlement_good_tv_price);
                TextView tvGoodnumber = holder.findViewById(R.id.item_setttlement_good_tv_count);
                tvGoodName.setText(entity.getProId().getTitle() != null ? entity.getProId().getTitle() : "kong");
                tvGoodPrice.setText(entity.getProId().getPriceNew() != null ? "￥" + entity.getProId().getPriceNew() : "kong");
                tvGoodnumber.setText(TextUtils.isEmpty(entity.getQuantitate() + "") ? "×" + entity.getQuantitate() : "×1");
                if (entity.getProId().getPicture().size() != 0) {
                    Glide.with(mContext).load(entity.getProId().getPicture().get(0)).into(imgShopper);
                }
                break;
            case SettlementViewModel.TYPE_SJ_FOOTER:
                TextView tvFooterWay = holder.findViewById(R.id.item_setttlement_footer_way);
                final EditText tvRemark = holder.findViewById(R.id.item_setttlement_tv_remark);
                TextView tvRecord = holder.findViewById(R.id.item_setttlement_tv_record);
                remark = tvRemark.getText().toString();
                Detail detail = (Detail) models.get(position).getObject();
                tvRecord.setText("共" + detail.getGoodsNum() + "件商品  小记￥" + detail.getOrderCountPrice() + "元");
                tvRemark.setTag(position);
                tvRemark.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View view, boolean b) {
                        if (b) {
                            int position = (int) view.getTag();
                            currentPosition = position;
                        }
                    }
                });
                tvRemark.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void afterTextChanged(Editable editable) {
                        remark = tvRemark.getText().toString();
                        for (int i = 1; currentPosition - i > 0; i++) {
                            int a = getItemViewType(currentPosition);
                            int upPosition = currentPosition - i;
                            if (getItemViewType(upPosition) == SettlementViewModel.TYPE_SJ) {
                                String shoper = models.get(upPosition).getObject().toString();
                                setRemark(shoper, remark);
                                break;
                            }
                        }
                    }
                });
                break;
            case SettlementViewModel.TYPE_TICKET:
                TextView tvTicket = holder.findViewById(R.id.items_ticket_tv);
                if (models.get(position).getObject() != null) {
                    String money = (String) models.get(position).getObject();
                    if (money.equals(mContext.getResources().getString(R.string.dialog_ticket_btn))) {
                        tvTicket.setText(money);
                    } else {
                        tvTicket.setText("已选择￥" + money + "元代金券");
                    }
                }
                tvTicket.setTag(position);
                tvTicket.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        myClickListener.click(view);
                    }
                });
                break;
            case SettlementViewModel.TYPE_PAY:
                alipayButton = holder.findViewById(R.id.checkbox_alipay);
                wechatButton = holder.findViewById(R.id.checkbox_wechat);
                bdButton = holder.findViewById(R.id.check_bd);
                RelativeLayout alipayRL, wechatRL, bdRL;
                alipayRL = holder.findViewById(R.id.Alipay);
                wechatRL = holder.findViewById(R.id.wechat);
                bdRL = holder.findViewById(R.id.bd);
                alipayButton.setChecked(true);
                initRadioButton();
                alipayRL.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (radio_check != CHECK_ALIPAY) {
                            radio_check = CHECK_ALIPAY;
                            initRadioButton();
                        }
                    }
                });
                wechatRL.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (radio_check != CHECK_WECHECK) {
                            radio_check = CHECK_WECHECK;
                            initRadioButton();
                        }

                    }
                });
                bdRL.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (radio_check != CHECK_DB) {
                            radio_check = CHECK_DB;
                            initRadioButton();
                        }
                    }
                });
                break;
        }
    }

    public void setRemark(String shoper, String detail) {
        boolean has = false;
        Remark remark = new Remark();
        remark.setShoper(shoper);
        remark.setDetail(detail);
        for (Remark hasRemark : remarkList) {
            if (shoper.equals(hasRemark.getShoper())) {
                hasRemark.setDetail(detail);
                has = true;
                break;
            }
        }
        if (!has) {
            remarkList.add(remark);
        }
    }

    public List<Remark> getRemarkList() {
        return remarkList;
    }

    private void initRadioButton() {
        switch (radio_check) {
            case CHECK_ALIPAY:
                alipayButton.setChecked(true);
                bdButton.setChecked(false);
                wechatButton.setChecked(false);
                pay_channel = Contents.ALIPAY;
                break;
            case CHECK_DB:
                alipayButton.setChecked(false);
                bdButton.setChecked(true);
                wechatButton.setChecked(false);
                pay_channel = Contents.BD;
                break;
            case CHECK_WECHECK:
                alipayButton.setChecked(false);
                bdButton.setChecked(false);
                wechatButton.setChecked(true);
                pay_channel = Contents.WX;
                break;
        }
    }

    public void setSelectTv(String money) {
        models.get(models.size() - 2).setObject(money);
    }

    public void setModels(List<SettlementViewModel> models) {
        this.models = models;
    }

    public List<SettlementViewModel> getModels() {
        return models;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.settlement_rl_address:
//
//                break;
        }

    }

    public interface MyClickListener {
        void click(View v);
    }
}


